package config;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;

import dao.ComisionDAO;
import dao.EmpleadoDAO;
import dao.PremioDAO;
import dao.ProductoDAO;
import dao.VentaDAO;
import dao.VentaXProductosDAO;
import impl.ComisionDAOImpl;
import impl.EmpleadoDAOImpl;
import impl.PremioDAOImpl;
import impl.ProductoDAOImpl;
import impl.VentaDAOImpl;
import impl.VentaXProductosDAOImpl;
import model.Comision;
import model.Empleado;
import model.Premio;
import model.Producto;
import model.Venta;
import model.VentaXProductos;


@Configuration
public class AppConfig {

	@Bean
	public EmpleadoDAO empleadoDao(){
		return new EmpleadoDAOImpl();
	}
	
	@Bean
	public ComisionDAO comisionDao(){
		return new ComisionDAOImpl();
	}
	
	@Bean
	public PremioDAO premioDao(){
		return new PremioDAOImpl();
	}
	
	@Bean
	public ProductoDAO productoDao(){
		return new ProductoDAOImpl();
	}
	
	@Bean
	public VentaDAO ventaDao(){
		return new VentaDAOImpl();
	}	
	@Bean
	public VentaXProductosDAO ventaxProductosDao(){
		return new VentaXProductosDAOImpl();
	}	
	

	@Bean
	public DataSource dataSource(){
		DriverManagerDataSource ret = new DriverManagerDataSource("jdbc:mysql://localhost:3306/TPSpringLM?createDatabaseIfNotExist=true", "root", "admin");
		ret.setDriverClassName("com.mysql.jdbc.Driver");
		return ret;
	}
	
	@Bean
	public AnnotationSessionFactoryBean sessionFactoryBean(){
		AnnotationSessionFactoryBean ret = new AnnotationSessionFactoryBean();
	
		//Clases anotadas con JPA.
		@SuppressWarnings("rawtypes")
		Class[] annotatedClasses = { Empleado.class, Comision.class, Premio.class, Producto.class, Venta.class, VentaXProductos.class };

		ret.setAnnotatedClasses(annotatedClasses);
		
		//Propiedades de hibernate.
		Properties hibernateProperties = new Properties();
		hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
		hibernateProperties.setProperty("hibernate.show_sql", "false");
		hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "update");
		ret.setHibernateProperties(hibernateProperties);
		
		ret.setDataSource(dataSource());
		return ret; 
	}
	
//	@Bean(name="transactionManager")
//	public JpaTransactionManager transactionManager(){
//		return new JpaTransactionManager();
//	}
}
